package com.huawei.app.demo.util;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;

import com.huawei.app.demo.MainActivity;
import com.huawei.app.demo.receiver.AppAddRemoveEventReceiver;
import com.huawei.app.demo.receiver.HDMIEventReceiver;
import com.huawei.app.demo.receiver.HomePressEventReceiver;

/**
 * 处理系统广播
 *
 * @since 2021-01-20
 */
public class BroadcastUtils {

    /**
     * 初始化广播事件处理
     */
    public static void initBroadcastReceiver(MainActivity activity) {
        // 处理HDMI插拔事件
        BroadcastReceiver hdmiReceiver = new HDMIEventReceiver(activity);
        IntentFilter hdmiStateFilter = new IntentFilter();
        hdmiStateFilter.addAction("com.device.action.HDMI_PLUGIN");
        hdmiStateFilter.addAction("com.device.action.HDMI_PLUGOUT");
        activity.registerReceiver(hdmiReceiver, hdmiStateFilter, "com.device.permission.THIRDAPP", null);

        // 处理Home按键事件
        BroadcastReceiver homePressEventReceiver = new HomePressEventReceiver();
        IntentFilter homePressFilter = new IntentFilter();
        homePressFilter.addAction("com.device.rc.action.PRESS_HOME_NOTIFY_THIRD_APP");
        activity.registerReceiver(homePressEventReceiver, homePressFilter, "com.device.permission.THIRDAPP", null);

        // 处理应用安装卸载升级事件
        BroadcastReceiver pkgChangeEventReceiver = new AppAddRemoveEventReceiver();
        IntentFilter pkgChangeFilter = new IntentFilter();
        pkgChangeFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
        pkgChangeFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        pkgChangeFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
        pkgChangeFilter.addDataScheme("package");
        activity.registerReceiver(pkgChangeEventReceiver, pkgChangeFilter);
    }
}
